<template>
  <div>
    <el-row>
      <el-col :span="10">
        <el-button
          type="success"
          @click="onGetnewData"
        >新增</el-button>
        <el-button
          type="primary"
          @click="onAllremove"
        >批量删除</el-button>
      </el-col>
      <el-col
        :span="8"
        :offset="6"
      >
        <el-input
          placeholder="请输入内容"
          v-model="input"
          class="input-with-select"
        >
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="onSearch"
          ></el-button>
        </el-input>
      </el-col>
    </el-row>

    <!-- 以下是表格部分 -->
    <el-row>
      <el-table
        :data="tableData"
        style="width: 100%;margin-top:26px"
        max-height="500"
        :default-sort="{ prop: 'date', order: 'descending' }"
        @sort-change="changeSort"
        @selection-change="handleSelectionChange"
        ref="index"
      >
        <!-- sort排序： ascending（上升）, descending（下降） -->
        <el-table-column
          fixed="left"
          type="selection"
          width="55"
        ></el-table-column>
        <el-table-column
          type="index"
          label="#"
          width="50"
        ></el-table-column>
        <el-table-column
          prop="title"
          label="标题"
          width="400"
        ></el-table-column>
        <el-table-column
          prop="author"
          label="作者"
          width="150"
        ></el-table-column>
        <el-table-column
          prop="date"
          label="日期"
          width="300"
          sortable
        ></el-table-column>
        <el-table-column
          prop="cid"
          label="类型"
          width="260"
        ></el-table-column>
        <el-table-column
          prop="time"
          label="时间"
          width="180"
          sortable
        ></el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="240"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="onLook(scope.row)"
            >查看</el-button>
            <el-button
              type="text"
              size="small"
              @click="onEdit(scope,scope.$index)"
            >修改</el-button>
            <el-button
              type="text"
              size="small"
              @click="onRemove(scope.row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>

    <!-- 分页 -->
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>

    <!-- 遮罩层部分 -->
    <el-drawer
      title="新增页面"
      :visible.sync="drawer"
      direction="rtl"
      size="66%"
    >
      <el-form
        :model="form"
        label-width="80px"
      >
        <el-form-item
          label="标题："
          prop="title"
        >
          <el-input
            v-model="form.title"
            :disabled="look"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="作者："
          prop="author"
        >
          <el-input
            v-model="form.author"
            :disabled="look"
          ></el-input>
        </el-form-item>
        <el-form-item label="类型：">
          <el-select
            :disabled="look"
            v-model="form.cid"
            placeholder="请选择类型"
          >
            <el-option
              v-for="(item, index) in classify"
              :key="index"
              :label="item.name"
              :value="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="时间：">
          <el-select
            :disabled="look"
            v-model="form.time"
            placeholder="请选择时间"
          >
            <el-option
              label="2分钟"
              value="1"
            ></el-option>
            <el-option
              label="3分钟"
              value="2"
            ></el-option>
            <el-option
              label="5分钟"
              value="3"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="内容："
          prop="content"
        >
          <mavonEditor
            style="max-height:100px"
            scrollStyle
            v-model="form.content"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            v-show="!look"
            type="primary"
            @click="onSubmit"
          >确定</el-button>
          <el-button @click="onReturn">返回</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import { mavonEditor } from "mavon-editor";
import "mavon-editor/dist/css/index.css";

import { mapState, mapActions, mapMutations } from "vuex";

export default {
  data() {
    return {
      form: {
        author: "situ",
      },
      drawer: false,
      input: "",
      look: false,
      arrAll: [],
      isColor: true,
      index: 0,
    };
  },
  created() {},
  mounted() {
    this.getlist();
  },
  methods: {
    ...mapActions("msbd", ["getlist", "insert", "updates", "delet"]),
    ...mapMutations("msbd", [
      "SET_PAGE",
      "SET_PAGSIZE",
      "SET_SORT",
      "SET_SEARCH",
    ]),

    // 搜索
    onSearch() {
      this.SET_SEARCH(this.input);
      this.getlist();
    },
    // 单个删除
    onRemove(val) {
      // console.log(val)
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.delet(val);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
    // 批量删除
    onAllremove() {
      this.onRemove(this.arrAll.join(","));
    },
    // 新增按钮
    onGetnewData() {
      this.drawer = true;
    },
    // 修改按钮
    onEdit(v, i) {
      this.index = i;
      this.drawer = true;
      this.form = { ...v.row };
    },
    // 查看
    onLook(v) {
      this.drawer = true;
      this.look = true;
      this.form = { ...v };
    },
    // 抽屉 / 确定
    onSubmit() {
      // console.log(this.form)
      if (this.form.id) {
        this.updates(this.form);
        this.form = {
          author: "situ",
        };
        // 淡入淡出
        // 1 从父获取子tr   // NO
        // 2 从按钮获取父tr // NO
        this.$refs.index.$el.children[2].children[0].children[1].children[
          this.index
        ].id = "active";
        setTimeout(() => {
          this.$refs.index.$el.children[2].children[0].children[1].children[
            this.index
          ].removeAttribute("id");
        }, 2600);
        this.$message({
          message: "您已修改成功",
          type: "success",
        });
      } else {
        this.insert(this.form);
        this.$message({
          message: "您已新增成功",
          type: "success",
        });
      }
      this.drawer = false;
    },
    // 排序改变
    changeSort(v) {
      this.SET_SORT(v);
      this.getlist();
    },
    // 多选
    handleSelectionChange(v) {
      this.arrAll = v.map((v) => {
        return v.id;
      });
      // console.log(this.arrAll);
    },
    // 页数change
    handleSizeChange(v) {
      this.SET_PAGSIZE(v);
      this.getlist();
    },
    handleCurrentChange(v) {
      this.SET_PAGE(v);
      this.getlist();
    },
    // 返回
    onReturn() {
      this.drawer = false;
      this.look = false;
      this.form = {
        author: "situ",
      };
    },
  },
  computed: {
    ...mapState("msbd", [
      "tableData",
      "classify",
      "currentPage",
      "pageSize",
      "total",
    ]),
  },
  components: {
    mavonEditor,
  },
};
</script>

<style scoped>
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.5s;
}
.fade-enter,
.fade-leave-to {
  background-color: #fff;
}
</style>

<style>
#active {
  animation: move 2.6s;
}

#el-drawer__title > span {
  outline: none;
}

@keyframes move {
  0% {
    background-color: rgb(255, 253, 137);
  }
  100% {
    background-color: #fff;
  }
}
</style>